//
//  PAAccountTool.m
//  PilotSeries
//
//  Created by 黄立强 on 16/4/14.
//  Copyright © 2016年 leon.huang. All rights reserved.
//

#import "PAAccountManager.h"
#import "PAUser.h"
#import "FMDB.h"

@implementation PAAccountManager
/** 数据库实例 */
static FMDatabase *_db;
NSString *const LQPAUsernameKey = @"PAUsername";
NSString *const LQPAPasswordKey = @"PAPassowrd";
NSString *const LQPAEnvironmentTypeKey = @"PAEnvironmentType";
NSString *const T_User_Env = @"t_user_env";
+ (void)initialize
{
    // 1.获得数据库文件的路径
    NSString *doc = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    NSString *filename = [doc stringByAppendingPathComponent:@"pilot.sqlite"];
    
    // 2.得到数据库
    _db = [FMDatabase databaseWithPath:filename];
    
    // 3.打开数据库
    if ([_db open]) {
        // 4.创表
        BOOL result = [_db executeUpdate:@"CREATE TABLE IF NOT EXISTS t_user_env (id integer PRIMARY KEY AUTOINCREMENT, username text NOT NULL, password text NOT NULL,env integer NOT NULL,group_name);"];
        if (result) {
            NSLog(@"成功创表");
        } else {
            NSLog(@"创表失败");
        }
        
        if(![_db columnExists:@"dock_size" inTableWithName:@"t_user_env"]){
            [_db executeUpdate:@"ALTER TABLE t_user_env ADD dock_size integer"];
            [_db executeUpdate:@"UPDATE t_user_env set dock_size = ? ",@9];
        }
    }
}


/**
 *  获取当前登录用户名及密码及环境
 *
 *  @return 当前登录用户名及密码及环境，不包括groupName
 */
+(PAUser *)currentPAUser{
    
    PAUser *user = [[PAUser alloc ] init];
    NSUserDefaults  *userSetting =  [NSUserDefaults standardUserDefaults];
    user.username = [userSetting stringForKey:LQPAUsernameKey];
    user.password = [userSetting stringForKey:LQPAPasswordKey];
    user.env =[userSetting integerForKey:LQPAEnvironmentTypeKey];
    
    return user;
}

/**
 *  保存当前登录用户名及密码及环境信息
 *
 *  @param pauser <#pauser description#>
 */
+(void)saveCurrentPAUser:(PAUser *)pauser{
    
    NSUserDefaults  *userSetting =  [NSUserDefaults standardUserDefaults];
    [userSetting setObject:pauser.username forKey:LQPAUsernameKey];
    [userSetting setObject:pauser.password forKey:LQPAPasswordKey];
    [userSetting setInteger:pauser.env forKey:LQPAEnvironmentTypeKey];
    [userSetting synchronize];
    
}



+(void)saveUser:(PAUser *)user{
    PAUser *pauser = [self loadUserByUsername:user.username withEnv:user.env];
    BOOL dflag;
    if (pauser!=nil) {
        //update
      dflag =  [_db executeUpdate:@"UPDATE t_user_env set group_name = ? where username = ? AND env = ?",pauser.groupName,user.username,@(user.env)];
    }else{
      dflag =   [_db executeUpdate:@"INSERT INTO t_user_env (username, password, group_name,env,dock_size) VALUES (?, ?, ?,?,?);",user.username,user.password,user.groupName,@(user.env),@9];
    }
    if (dflag) {
        LOG(@"saveUser success!")
    }else{
        LOG(@"saveUser fail!")
    }
    
}

+(PAUser *)loadUserByUsername:(NSString *)username withEnv:(NSInteger )env{
    
      FMResultSet *resultSet = nil;
      resultSet = [_db executeQuery:@"SELECT * FROM t_user_env WHERE username = ? AND env = ? ;",username,@(env)];
    PAUser *user = nil;
    // 遍历查询结果
    while (resultSet.next) {
        NSString *groupName = [resultSet stringForColumn:@"group_name"];
        NSString *password = [resultSet stringForColumn:@"password"];
        user = [PAUser new];
        user.username =username;
        user.env = env;
        user.password = password;
        user.groupName = groupName;
    }
    return user;
    
}

+(NSString *)loadCurrentGroupName{
    FMResultSet *resultSet = nil;
      PAUser *user = [self currentPAUser];
    NSString *groupName;
    resultSet = [_db executeQuery:@"SELECT * FROM t_user_env WHERE username = ? AND env = ?;",user.username,@(user.env)];
    // 遍历查询结果
    while (resultSet.next) {
        
       groupName = [resultSet stringForColumn:@"group_name"];
        
    }
//    if(groupName==nil) groupName = @"Default";
    return groupName;
}

+(void) saveGroupName:(NSString *) groupName{

    PAUser *user = [self currentPAUser];
    BOOL dflag =  [_db executeUpdate:@"UPDATE t_user_env set group_name = ? where username = ? AND env = ?",groupName,user.username,@(user.env)];
    if (dflag) {
        LOG(@"saveGroupName success!")
    }else{
        LOG(@"saveGroupName fail!")
    }
    
    
}

+(void) saveDockSize:(NSInteger )dockSize{
     PAUser *user = [self currentPAUser];
    BOOL dflag =  [_db executeUpdate:@"UPDATE t_user_env set dock_size = ? where username = ? AND env = ?",@(dockSize),user.username,@(user.env)];
    if (dflag) {
        LOG(@"saveDockSize success!")
    }else{
        LOG(@"saveDockSize fail!")
    }
    
}

+(NSInteger )loadCurrentDockSize{
    FMResultSet *resultSet = nil;
    PAUser *user = [self currentPAUser];
    NSInteger dockSize = 0;
    resultSet = [_db executeQuery:@"SELECT * FROM t_user_env WHERE username = ? AND env = ?;",user.username,@(user.env)];
    // 遍历查询结果
    while (resultSet.next) {
        
        dockSize = [resultSet longForColumn:@"dock_size"];
        
    }
    
    return dockSize;
}

+(NSString *)getSizeTypeString:(PACollectionSizeType )collectionSizeType {
	
    switch (collectionSizeType) {
        case PACollectionSizeType1X2:{
            return @"1x2";
            break;
        }
        case PACollectionSizeType1X3:{
            return @"1x3";
            break;
        }
        case PACollectionSizeType2X2: {
            return @"2x2";
            break;
        }
        case PACollectionSizeType1X5:{
            return @"1x5";
            break;
        }
        case PACollectionSizeType2X3: {
            return @"2x3";
            break;
        }
        case PACollectionSizeType2X4: {
            return @"2x4";
            break;
        }
        case PACollectionSizeType2X5: {
            return @"2x5";
            break;
        }
        case PACollectionSizeType3X3: {
            return @"3x3";
            break;
        }
        case PACollectionSizeType3X4: {
            return @"3x4";
            break;
        }
        case PACollectionSizeType3X5: {
            return @"3x5";
            break;
        }
        case PACollectionSizeType4X4: {
            return @"4x4";
            break;
        }
    }

}
@end
